package org.dream.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.dream.dto.LongTou;
import org.dream.persistence.PersistenceService;
import org.dream.persistence.PersistenceServiceImpl;

/**
 * 股票列表面板
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class LongTouTablePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTable stockTable;

	private PersistenceService persistenceService = new PersistenceServiceImpl();
	private boolean calculate = false;

	public LongTouTablePanel() {
		super(new BorderLayout());
		addStockDataTable();// 初始化股票数据表格

	}

	/**
	 * 添加表格列名称
	 */
	private Vector<String> getColumnNames() {
		Vector<String> columnNames = new Vector<String>();
		columnNames.add("序号");
		columnNames.add("板块名称");
		columnNames.add("龙头一");
		columnNames.add("次数");
		columnNames.add("龙头二");
		columnNames.add("次数");
		columnNames.add("龙头三");
		columnNames.add("次数");
		columnNames.add("龙头四");
		columnNames.add("次数");
		columnNames.add("龙头五");
		columnNames.add("次数");
		columnNames.add("龙头六");
		columnNames.add("次数");
		return columnNames;
	}

	/**
	 * 得到股票表格数据
	 */
	public Vector<Vector<Object>> getStockTableRowData() {
		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		Map<String, List<LongTou>> map = persistenceService.findLongTous();
		int i = 0;
		for (String key : map.keySet()) {
			List<LongTou> list = map.get(key);
			Vector<Object> row = new Vector<Object>();
			row.add("" + (++i));
			row.add(list.get(0).getBkmc());
			for (int j = 0; j < (list.size() > 6 ? 6 : list.size()); j++) {
				row.add(list.get(j).getLongTou());
				row.add(list.get(j).getCount());
			}

			data.add(row);
		}
		return data;
	}

	/**
	 * 初始化股票数据表格
	 */
	private void addStockDataTable() {

		stockTable = new JTable(getStockTableRowData(), getColumnNames());
		stockTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (stockTable.getSelectedRow() == -1) {
					return;
				}

			}
		});
		stockTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
		add(new JScrollPane(stockTable), BorderLayout.CENTER);
	}

	/**
	 * 更新数据表格数据
	 */
	private void refreshStockDataTable() {
		DefaultTableModel tableModel = new DefaultTableModel(getStockTableRowData(), getColumnNames());
		stockTable.setModel(tableModel);
		stockTable.updateUI();
	}
}
